// 导入可视化库 {
const { Tracer, GraphTracer, LogTracer, Layout, VerticalLayout } = require('algorithm-visualizer');
// }

const G = [ // G[i][j] 定义邻接矩阵
  [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
];

// 定义 tracer 变量 {
const tracer = new GraphTracer();
const logger = new LogTracer();
Layout.setRoot(new VerticalLayout([tracer, logger]));
tracer.log(logger);
tracer.set(G);
tracer.layoutTree(0);
Tracer.delay();
// }

// 这是一个简单的DLS（深度限制搜索）示例
// 我们尝试在限制的深度内搜索数据
function DLSCount(limit, node, parent) { // limit = 深度限制 node = 当前节点, parent = 前置节点
  // visualize {
  tracer.visit(node, parent);
  Tracer.delay();
  // }
  let child = 0;
  if (limit > 0) { // 剪枝
    for (let i = 0; i < G[node].length; i++) {
      if (G[node][i]) { // 如果i号节点是当前node节点的子节点
	  child += 1 + DLSCount(limit - 1, i, node); // 递归调用DLS函数
      }
    }
    return child;
  }
  return child;
}

// logger {
logger.println(`0号节点的后代数量一共有：${DLSCount(2, 0)}个。`);
// }
